import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:kq_flutter_core_widget/utils/kq_screen_util.dart';
import 'package:kq_flutter_core_widget/widgets/button/kq_ink_well.dart';
import 'package:kq_flutter_core_widget/widgets/image/kq_image.dart';
import 'package:kq_flutter_tv_widgets/config/kq_global.dart';
import 'package:kq_flutter_tv_widgets/resources/kq_theme_colors.dart';
import 'package:kq_flutter_tv_widgets/resources/l10n/kq_intl.dart';
import '../../resources/images.dart';
import 'input_opt_type.dart';

///数字键盘
class InputNumberView extends StatelessWidget {
  ///键之间的间距
  final double? gap;

  ///键宽
  final double? width;

  ///键高
  final double? height;

  ///controller
  final InputNumberViewController? controller;

  const InputNumberView({
    super.key,
    this.gap,
    this.width,
    this.height,
    this.controller,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      alignment: Alignment.center,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              _buildItem(InputOptType.number, 1),
              SizedBox(width: gap ?? 16.r),
              _buildItem(InputOptType.number, 2),
              SizedBox(width: gap ?? 16.r),
              _buildItem(InputOptType.number, 3),
            ],
          ),
          SizedBox(height: gap ?? 16.r),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              _buildItem(InputOptType.number, 4),
              SizedBox(width: gap ?? 16.r),
              _buildItem(InputOptType.number, 5),
              SizedBox(width: gap ?? 16.r),
              _buildItem(InputOptType.number, 6),
            ],
          ),
          SizedBox(height: gap ?? 16.r),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              _buildItem(InputOptType.number, 7),
              SizedBox(width: gap ?? 16.r),
              _buildItem(InputOptType.number, 8),
              SizedBox(width: gap ?? 16.r),
              _buildItem(InputOptType.number, 9),
            ],
          ),
          SizedBox(height: gap ?? 16.r),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              _buildItem(InputOptType.clear, null),
              SizedBox(width: gap ?? 16.r),
              _buildItem(InputOptType.number, 0),
              SizedBox(width: gap ?? 16.r),
              _buildItem(InputOptType.delete, null),
            ],
          ),
        ],
      ),
    );
  }

  Widget _buildItem(InputOptType type, int? number) {
    return KqInkWell(
      onTap: () {
        (controller ?? InputNumberViewController()).onOpt?.call(type, number);
      },
      child: Container(
        width: width ?? 88.r,
        height: height ?? 72.r,
        alignment: Alignment.center,
        decoration: BoxDecoration(
          color: KqThemeColors.bgBlack05,
          borderRadius: BorderRadius.circular(4.r),
        ),
        child: type == InputOptType.number
            ? Text(
                number.toString(),
                style: TextStyle(
                  color: KqThemeColors.textBlack85,
                  fontSize: 35.sp,
                ),
              )
            : type == InputOptType.clear
                ? Text(
                    KqIntl.currentResource.clear2,
                    style: TextStyle(
                      color: KqThemeColors.textBlue,
                      fontSize: 20.sp,
                    ),
                  )
                : Align(
                    child: KqImage.assets(
                      url: Images.commonIcDelete40,
                      package: KqGlobal.packageName,
                      width: 40.r,
                      height: 40.r,
                    ),
                  ),
      ),
    );
  }
}

class InputNumberViewController {
  ///操作回调
  Function(InputOptType type, int? input)? onOpt;
}
